/*
 * @lc app=leetcode id=475 lang=cpp
 *
 * [475] Heaters
 */

// @lc code=start
class Solution {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        int j = 0;
        int ans = 0;
        for (int i = 0, j = 0; i < houses.size(); ++i) {
            while  ( j < heaters.size() - 1 && houses[i] >  heaters[j])  {
                j++;
            }  
            int a = INT_MAX;   
            if ( j > 0) {
               a =  std::abs(houses[i] - heaters[j-1]);
            }
            int b = std::abs(houses[i] - heaters[j]);
            int c = std::min(a, b);
            ans = std::max(ans, c);         

        }
        return ans;
    }
};
// @lc code=end

